package com.personal.portal.admin.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.personal.portal.common.entity.Result;
import com.personal.portal.admin.entity.Setting;
import com.personal.portal.admin.mapper.SettingMapper;
import com.personal.portal.admin.service.SettingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统设置服务实现类
 * 
 * @author zhanghui
 * @version 1.0.0
 * @since 2025-11-10
 */
@Service
public class SettingServiceImpl implements SettingService {

    @Resource
    private SettingMapper settingMapper;

    @Override
    public Setting queryById(Long id) {
        validateId(id);
        return settingMapper.selectOneById(id);
    }

    @Override
    public Setting queryByEntity(Setting setting) {
        QueryWrapper queryWrapper = buildQueryWrapper(setting);
        return settingMapper.selectOneByQuery(queryWrapper);
    }

    @Override
    public List<Setting> queryList(Setting setting) {
        QueryWrapper queryWrapper = buildQueryWrapper(setting);
        return settingMapper.selectListByQuery(queryWrapper);
    }

    @Override
    public Result.PageResult<Setting> queryPage(int pageNum, int pageSize, Setting setting) {
        QueryWrapper queryWrapper = buildQueryWrapper(setting);
        Page<Setting> paginate = settingMapper.paginate(pageNum, pageSize, queryWrapper);
        return new Result.PageResult<>(paginate.getRecords(), paginate.getTotalRow(), pageNum, pageSize);
    }

    @Override
    public int insert(Setting setting) {
        validateSetting(setting);
        return settingMapper.insert(setting);
    }

    @Override
    public int update(Setting setting) {
        if (setting.getId() == null) {
            throw new IllegalArgumentException("系统设置ID不能为空");
        }
        validateId(setting.getId());
        validateSetting(setting);
        
        Setting existing = settingMapper.selectOneById(setting.getId());
        if (existing == null) {
            throw new IllegalArgumentException("系统设置信息不存在");
        }
        
        return settingMapper.update(setting);
    }

    @Override
    public int deleteById(Long id) {
        validateId(id);
        
        Setting existing = settingMapper.selectOneById(id);
        if (existing == null) {
            throw new IllegalArgumentException("系统设置信息不存在");
        }
        
        return settingMapper.deleteById(id);
    }

    @Override
    public List<Long> deleteBatch(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new IllegalArgumentException("ID列表不能为空");
        }

        List<Long> successIds = new ArrayList<>();
        List<Long> failedIds = new ArrayList<>();

        for (Long id : ids) {
            try {
                validateId(id);
                
                Setting existing = settingMapper.selectOneById(id);
                if (existing == null) {
                    failedIds.add(id);
                    continue;
                }

                int result = settingMapper.deleteById(id);
                if (result > 0) {
                    successIds.add(id);
                } else {
                    failedIds.add(id);
                }
            } catch (Exception e) {
                failedIds.add(id);
            }
        }

        if (!successIds.isEmpty()) {
            return successIds;
        } else {
            throw new IllegalArgumentException("批量删除失败");
        }
    }

    /**
     * 验证系统设置信息
     */
    private void validateSetting(Setting setting) {
        if (setting == null) {
            throw new IllegalArgumentException("系统设置信息不能为空");
        }
        if (StringUtils.isBlank(setting.getSettingKey())) {
            throw new IllegalArgumentException("设置键不能为空");
        }
        if (StringUtils.isBlank(setting.getSettingValue())) {
            throw new IllegalArgumentException("设置值不能为空");
        }
        if (StringUtils.isBlank(setting.getSettingType())) {
            throw new IllegalArgumentException("设置类型不能为空");
        }
    }

    /**
     * 验证ID
     */
    private void validateId(Long id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("无效的系统设置ID");
        }
    }

    /**
     * 构建查询条件
     */
    private QueryWrapper buildQueryWrapper(Setting setting) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(Setting::getId, setting.getId(), setting.getId() != null);
        queryWrapper.like(Setting::getSettingKey, setting.getSettingKey(), StringUtils.isNotEmpty(setting.getSettingKey()));
        queryWrapper.like(Setting::getSettingValue, setting.getSettingValue(), StringUtils.isNotEmpty(setting.getSettingValue()));
        queryWrapper.like(Setting::getSettingType, setting.getSettingType(), StringUtils.isNotEmpty(setting.getSettingType()));
        queryWrapper.like(Setting::getGroupName, setting.getGroupName(), StringUtils.isNotEmpty(setting.getGroupName()));
        queryWrapper.eq(Setting::getIsSystem, setting.getIsSystem(), setting.getIsSystem() != null);

        queryWrapper.orderBy(Setting::getSortOrder, true);
        queryWrapper.orderBy(Setting::getSettingKey, true);
        queryWrapper.orderBy(Setting::getId, true);
        return queryWrapper;
    }
}